package com.huadao.cart_ccs.experiment.concentrationReplacement;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseDialogFragment;
import com.huadao.cart_ccs.base.BasePresenter;
import com.huadao.cart_ccs.common.TimeUtil;
import com.huadao.cart_ccs.data.task.ChangeLiquidTask;
import com.huadao.cart_ccs.main.CartApplication;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class ConcentrationReplacementDF extends BaseDialogFragment {

    public interface OnConcentrationReplacement {
        void concentrationReplacement(int fluidAmount, int wasteAmount, int numbers,Date date,String user);

        void terminateConcentrationReplacement();
    }

    @BindView(R.id.et_waste_amount)
    EditText etWasteAmount;
    @BindView(R.id.et_fluid_amount)
    EditText etFluidAmount;
    @BindView(R.id.et_numbers)
    EditText etNumbers;
    @BindView(R.id.et_date)
    EditText etDate;
    @BindView(R.id.et_time)
    EditText etTime;
    @BindView(R.id.et_operator)
    EditText etOperator;
    @BindView(R.id.btn_stop_operate)
    Button btnStopOperate;
    @BindView(R.id.btn_operate)
    Button btnOperate;

    private ChangeLiquidTask changeLiquidTask;
    private OnConcentrationReplacement onConcentrationReplacement;

    public static ConcentrationReplacementDF newInstance(ChangeLiquidTask changeLiquidTask) {
        ConcentrationReplacementDF concentrationReplacementDF = new ConcentrationReplacementDF();
        Bundle bundle = new Bundle();
        bundle.putSerializable("changeLiquidTask", changeLiquidTask);
        concentrationReplacementDF.setArguments(bundle);
        return concentrationReplacementDF;
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialogfrag_concentration_replacement;
    }

    private Date date;
    private String user;
    @Override
    protected void onInit() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            changeLiquidTask = (ChangeLiquidTask) bundle.get("changeLiquidTask");
        }
        this.getDialog().setCanceledOnTouchOutside(false);

        date = new Date();
        user = CartApplication.getInstance().getOperator();

        etDate.setEnabled(false);
        etTime.setEnabled(false);
        etOperator.setEnabled(false);
        if (changeLiquidTask != null) {
            etWasteAmount.setText(String.valueOf(changeLiquidTask.getChangeLiquidBO().getChangeLiquidTurnList().get(0).getAmountList().get(0).getFluid()));
            etFluidAmount.setText(String.valueOf(changeLiquidTask.getChangeLiquidBO().getChangeLiquidTurnList().get(0).getAmountList().get(0).getWaste()));
            etNumbers.setText(String.valueOf(changeLiquidTask.getChangeLiquidBO().getChangeLiquidTurnList().get(0).getAmountList().size()));
            etDate.setText(TimeUtil.date_sdf.format(changeLiquidTask.getOperateDate()));
            etTime.setText(TimeUtil.time_sdf.format(changeLiquidTask.getOperateDate()));
            etOperator.setText(changeLiquidTask.getOperator());
            etWasteAmount.setEnabled(false);
            etFluidAmount.setEnabled(false);
            etNumbers.setEnabled(false);
            btnStopOperate.setVisibility(View.GONE);
            btnOperate.setEnabled(false);
        } else {
            etWasteAmount.setEnabled(true);
            etFluidAmount.setEnabled(true);
            etNumbers.setEnabled(true);
            etOperator.setText(user);
            etDate.setText(TimeUtil.date_sdf.format(date));
            etTime.setText(TimeUtil.time_sdf.format(date));
        }
    }

    @Override
    protected boolean isDialog() {
        return true;
    }

    @OnClick({R.id.btn_close, R.id.btn_stop_operate, R.id.btn_operate})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
                this.dismiss();
                break;
            case R.id.btn_stop_operate:
                if(onConcentrationReplacement!=null){
                    onConcentrationReplacement.terminateConcentrationReplacement();
                }
                this.dismiss();
                break;
            case R.id.btn_operate:
                String err=check();
                if(TextUtils.isEmpty(err)){
                    if(onConcentrationReplacement!=null){
                        onConcentrationReplacement.concentrationReplacement(Integer.parseInt(etFluidAmount.getText().toString()),Integer.parseInt(etWasteAmount.getText().toString()),Integer.parseInt(etNumbers.getText().toString()),date,user);
                        etFluidAmount.setEnabled(false);
                        etWasteAmount.setEnabled(false);
                        etFluidAmount.setEnabled(false);
                        etNumbers.setEnabled(false);
                        etDate.setEnabled(false);
                        etTime.setEnabled(false);
                        etOperator.setEnabled(false);
                        btnStopOperate.setVisibility(View.VISIBLE);
                        btnOperate.setText("浓缩置换中...");
                        btnOperate.setEnabled(false);
                    }else {
                        this.dismiss();
                    }
                }else {
                    showDialogMessage("",err);
                }
                break;
        }
    }

    private String check() {
        String err = null;

        if (TextUtils.isEmpty(etWasteAmount.getText())) {
            err = "请输入排液体积";
        } else if (TextUtils.isEmpty(etFluidAmount.getText())) {
            err = "请输入进液体积";
        } else if (TextUtils.isEmpty(etNumbers.getText())) {
            err = "请输入置换次数";
        }else if (Integer.parseInt(etWasteAmount.getText().toString())<=0) {
            err = "排液体积必须大于0";
        } else if (Integer.parseInt(etFluidAmount.getText().toString())<=0) {
            err = "进液体积必须大于0";
        } else if (Integer.parseInt(etNumbers.getText().toString())<=0) {
            err = "置换次数必须大于0";
        }
        return err;
    }

    public OnConcentrationReplacement getOnConcentrationReplacement() {
        return onConcentrationReplacement;
    }

    public void setOnConcentrationReplacement(OnConcentrationReplacement onConcentrationReplacement) {
        this.onConcentrationReplacement = onConcentrationReplacement;
    }
}
